package scau.edu.cn.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import scau.edu.cn.common.utils.ApiResponse;
import scau.edu.cn.form.CollectForm;
import scau.edu.cn.form.Info;
import scau.edu.cn.form.InfoType;
import scau.edu.cn.service.JudgeInfoService;
import scau.edu.cn.service.JudgeInfoTypeService;
import scau.edu.cn.service.QueryService;

import java.util.List;

/**
 * @description: 收集控制器
 * @author: yuxiang
 * @create: 2020-08-14 20:41
 **/
@RestController
//@RequestMapping("/collect")
public class CollectController {

    /**
     * 图片服务
     */
    @Autowired
    private JudgeInfoService judgeInfoService;

    /**
     * 问卷服务
     */
    @Autowired
    private QueryService queryService;

    /**
     * 评分分类数据表
     */
    @Autowired
    private JudgeInfoTypeService judgeInfoTypeService;

    @PostMapping("/collect")
    public ApiResponse collectInfo(@RequestBody List<Info> infos){
        ApiResponse response = new ApiResponse();

        try {
            response = judgeInfoService.saveList(infos);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResponse(-1,"服务器错误");
        }
        return response;
    }

    @PostMapping("/collectByType")
    public ApiResponse collectInfoType(@RequestBody List<InfoType> infos){
        ApiResponse response = new ApiResponse();

        try {
            response = judgeInfoTypeService.saveList(infos);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResponse(-1,"服务器错误");
        }
        return response;
    }

    @GetMapping("/create")
    public ApiResponse createQuery(){
        ApiResponse response = new ApiResponse();

        try {
            response = queryService.createQuery(0);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResponse(-1,"服务器错误");
        }
        return response;
    }

    /**
     * 按分类来创建问卷
     * @param type
     * @return
     */
    @GetMapping("/create/{type}")
    public ApiResponse createQuery(@PathVariable String type){
        ApiResponse response = new ApiResponse();

        try {
            response = queryService.createQueryByType(type);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResponse(-1,"服务器错误");
        }
        return response;
    }

    @GetMapping("/check")
    public ApiResponse check(@RequestParam("id") String id){
        ApiResponse response = new ApiResponse();
        try {
            System.out.println("验证id===>"+id);
            response = queryService.checkQuery(id);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResponse(-1,"服务器错误");
        }
        return response;
    }
}
